function getCommentList() {
  $.ajax({
    method: "GET",
    url: "http://www.liulongbin.top:3006/api/cmtlist",
    success: function (res) {
      console.log(res);
      if (res.status !== 200) {
        return alert("s");
      }
      var rows = [];
      $.each(res.data, function (i, item) {
        // console.log(item);
        var str = `<li class="list-group-item">
        <a href="javascript:;" class="badge" style="background-color: #d9534f" data-id="${item.id}">删除</a>
        <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
        <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
        ${item.content}
        
      </li>`;
        rows.push(str);
      });
      $("#cmt-list").empty().append(rows.join(""));
    },
  });
}
getCommentList();
$(function () {
  $("#formAddCmt").submit(function (e) {
    e.preventDefault();
    var data = $(this).serialize();
    console.log(data);
    $.ajax({
      method: "post",
      url: "http://www.liulongbin.top:3006/api/addcmt",
      data: data,
      success: function (res) {
        if (res.status !== 201) {
          return alert("s");
        }
        getCommentList();
        $("#formAddCmt")[0].reset();
      },
    });
  });
  $("ul" /* 因为li也是后来创建的所以没有办法做点击事件 */).on(
    "click",
    "a",
    function () {
      let id = $(this).attr("data-id");
      // console.log(id);
      $.ajax({
        method: "DELETE",
        url: `http://www.liulongbin.top:3006/api/delcmt?id=${id}`,

        success: function (res) {
          console.log(res);
          if (res.status !== 200) {
            return alert(res.msg);
          }
          getCommentList();
        },
      });
    }
  );
});
